<?php

class Jf_File_Image {

    // 获取图片信息
    public static function getInfo ($fname) {
        $flag = (is_string($fname) && is_file($fname)) ? true : fasle;
        $tmp = $flag ? getimagesize ($fname) : false;
        $tmp = is_array($tmp) ? $tmp : false;
        $info = false;
        if($tmp){
            $info = array('w' => $tmp[0], 'h' => $tmp[1], 'ext' => '');
            switch($tmp[2]){
                case 1:
                    $info['ext'] = 'gif';
                    break;
                case 2:
                    $info['ext'] = 'jpeg';
                    break;
                case 3:
                    $info['ext'] = 'png';
                    break;
                case 6:
                    $info['ext'] = 'bmp';
                    break;
                default:
                    $info = false;
                    break;
            }
            unset($tmp);
        }
        return $info;
    }

    // 创建图片源
    public static function createResource ($fname) {
        $info = self::getInfo ($fname);
        $resource = false;
        if($info){
            switch ($info['ext']) {
                case 'jpeg' :
                    $res = imagecreatefromjpeg ($fname);
                    $resource = $res ? array('ext' => 'jpeg', 'res' => $res) : fasle;
                    unset($res);
                    break;
                case 'gif' :
                    $res = imagecreatefromgif ($fname);
                    $resource = $res ? array('ext' => 'gif', 'res' => $res) : fasle;
                    unset($res);
                    break;
                case 'png' :
                    $res = imagecreatefrompng ($fname);
                    $resource = $res ? array('ext' => 'png', 'res' => $res) : fasle;
                    unset($res);
                    break;
                case 'bmp' :
                    $res = imagecreatefromwbmp ($fname);
                    $resource = $res ? array('ext' => 'bmp', 'res' => $res) : fasle;
                    unset($res);
                    break;
                default:
                    $resource = false;
                    break;
            }
            unset($info);
        }
        return $resource;
    }

    /**
     * 生成图片文件
     * @param string $resource 图片资源
     * @param string $fname 要保存的文件路径
     * @param bool $result 生成缩略图状态 true:成功; false:失败;
     */
    public static function createFile ($resource, $fname) {
        $res = $resource && is_array($resource) && isset($resource['res']) ? $resource['res'] : false;
        $ext = $resource && is_array($resource) && isset($resource['ext']) ? $resource['ext'] : false;
        $result = false;
        if($res && $ext){
            switch ($ext) {
                case 'jpeg' :
                    $result = imagejpeg ($res, $fname);
                    break;
                case 'gif' :
                    $result = imagegif ($res, $fname);
                    break;
                case 'png' :
                    $result = imagepng ($res, $fname);
                    break;
                case 'bmp' :
                    $result = imagewbmp ($res, $fname);
                    break;
            }
            imagedestroy($res);
        }
        return $result;
    }

    // 更改大小
    public static function resize ($fname, $w, $h, $cut = 0) {
        $cut = $cut && is_int($cut) && (intval($cut) > 0) ? intval($cut) : 0;
        $w = $w && is_int($w) && (intval($w) > 0) ? intval($w) : false;
        $h = $h && is_int($h) && (intval($h) > 0) ? intval($h) : false;

        $info = $fname && is_file($fname) ? self::getInfo ($fname) : false;
        $ext = $info && isset($info['ext']) ? $info['ext'] : false;
        $wo = $info && isset($info['w']) && intval($info['w']) ? intval($info['w']) : false;
        $ho = $info && isset($info['h']) && intval($info['h']) ? intval($info['h']) : false;

        $target = false;
        if($ext && $w && $h && $wo && $ho){
            $ro = $ho/$wo;
            $r = $h/$w;
            $source = self::createResource($fname);
            $source = $source && isset($source['res']) ? $source['res'] : false;
            if($source){
                $x = $y = 0;
                if($cut > 0){ // 强制宽高（切图）
                    if ($ro >= $r) { // 过高
                        $hoo = $wo * $r;
                        $y = ($ho - $hoo)/2;
                        $copy = imagecreatetruecolor ($wo, $hoo);
                        imagecopy ($copy, $source, 0, 0, $x, $y, $wo, $ho);
                        $source = $copy;
                        $ho = $hoo;
                    } else { // 过宽
                        $woo = $ho / $r;
                        $x = ($wo - $woo)/2;
                        $copy = imagecreatetruecolor ($woo, $ho);
                        imagecopy ($copy, $source, 0, 0, $x, $y, $wo, $ho);
                        $source = $copy;
                        $wo = $woo;
                    }
                }else{ // （不强制）不切图
                    if ($ro >= $r) { // 过宽
                        $h = intval($w/$ro);
                    } else { // 过宽
                        $w = intval($h*$ro);
                    }
                }
                $target = imagecreatetruecolor ($w, $h);
                // 图片失真处理
                imagealphablending($target , false);
                imagesavealpha($target, true);
                imagecopyresampled ($target, $source, 0, 0, 0, 0, $w, $h, $wo, $ho);
                imagecolorallocatealpha($target, 255, 255, 255, 0);
            }
            $target = array('ext' => $ext, 'res' => $target);
            // 生成图片文件
            $target = self::createFile ($target, $fname);
        }
        return $target;
    }

    // 旋转图片到原始方向
    public static function rotate ($fname) {
        $exif = is_file($fname) ? exif_read_data ($fname) : fasle;
        if ($exif && is_array($exif) && isset($exif['Orientation'])) {
            switch ($exif['Orientation']) {
                case 8 :
                    $resource = self::createResource ($fname);
                    $rotate = imagerotate ($resource, 90, 0);
                    self::createFile ($rotate, $fname);
                    break;
                case 3 :
                    $resource = self::createResource ($fname);
                    $rotate = imagerotate ($resource, 180, 0);
                    self::createFile ($rotate, $fname);
                    break;
                case 6 :
                    $resource = self::createResource ($fname);
                    $rotate = imagerotate ($resource,  - 90, 0);
                    self::createFile ($rotate, $fname);
                    break;
            }
        }
    }

}
